<template>
  <div class="goodlist">
    <div class="search">
      <div class="search_input">
        <el-input placeholder="请输入内容" clearable v-model="params.query">
          <template #append>
            <el-button :icon="Search" @click="initgoodslist" />
          </template>
        </el-input>
      </div>
      <div class="search_btn">
        <el-button type="primary" @click="">添加商品</el-button>
      </div>
    </div>
    <div class="table">
      <el-table
        :data="Goods"
        :border="true"
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column
          v-for="(item, index) in GoodsTitle"
          :key="index"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
        >
          <template v-slot="{ row }" v-if="item.prop === 'action'">
            <el-button type="primary" :icon="Edit" size="small">
              编辑
            </el-button>
            <el-button type="danger" :icon="Delete" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pageation">
      <el-pagination
        v-model:currentPage="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 4, 6, 8]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import apis from "@/http/apis";
import { GoodsTitle } from "@/utils/userTable";
import { Delete, Edit, Search, Setting } from "@element-plus/icons-vue";
import { ref, shallowReactive } from "vue";

let Goods: Idata = ref([]);
//初始请求参数对象
const params = shallowReactive({
  query: "",
  pagenum: 1,
  pagesize: 10,
});
const total = ref(0); //分页部分总分页数

const initgoodslist = async () => {
  const res: any = await apis.getGoodlist(params);

  Goods.value = res.data.goods;
  total.value = res.data.total;

  console.log(Goods.value, "列表");
};
initgoodslist();
//pageSize改变处理函数
const handleSizeChange = (pagesize: number) => {
  params.pagenum = 1; //重置页数
  params.pagesize = pagesize; //将改变后的值重新赋值
  initgoodslist();
};
//pagenum改变处理函数
const handleCurrentChange = (pagenum: number) => {
  params.pagenum = pagenum;
  initgoodslist();
};
</script>

<style lang="less" scoped>
.goodlist {
  .search {
    display: flex;
    .search_input {
      width: 30%;
    }
    .search_btn {
      margin-left: 15px;
    }
  }
  .table {
    margin-top: 10px;
  }
  .pageation {
    width: 100%;
    background: #fff;
    padding: 10px 0;
  }
}
</style>
